package com.dp.appi.service.impl;

import com.dp.appi.dao.EnvironmentDao;
import com.dp.appi.dao.ExplorationDao;
import com.dp.appi.exception.CommonException;
import com.dp.appi.service.EnvironmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by bchen on 2016/5/19.
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class EnvironmentServiceImpl implements EnvironmentService{
    @Autowired
    private EnvironmentDao environmentDao;

    @Autowired
    private ExplorationDao explorationDao;


    /**
     * 获取踏勘环境信息
     *
     * @param exploration_id
     */
    @Override
    public Map<String, Object> envInfo(Long exploration_id) {
        return environmentDao.envInfo(exploration_id);
    }

    /**
     * 保存踏勘环境信息
     *
     * @param envMap
     */
    @Override
    public void saveEnv(Map<String,String> envMap) {
        Long exploration_id = Long.parseLong(String.valueOf(envMap.get("exploration_id")));
        if(!explorationDao.isExistFeature(exploration_id,11)){
            throw new CommonException("EXPO007");
        }
        //检查是否存在该踏勘的环境信息
        if(environmentDao.isExistEnv(exploration_id)){//存在，update
            environmentDao.upEnvInfo(envMap);
        }else{//不存在，add
            environmentDao.addEnvInfo(envMap);
        }

    }
}
